<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="residenceStatus">
        <el-radio-group v-model="queryParams.residenceStatus">
          <el-radio value="">全部</el-radio>
          <el-radio :value="0">常住</el-radio>
          <el-radio :value="1">暂住</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['crowdInfo:crowd:export']"
          >下载</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="tableList" border>
      <el-table-column label="序号" type="index" align="center" width="70" />
      <el-table-column label="类型" prop="residenceStatus" align="center">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.residenceStatus == 0"
            >常住</el-tag
          >
          <el-tag type="warning" v-if="scope.row.residenceStatus == 1"
            >暂住</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="姓名" prop="userName" align="center" />
      <el-table-column label="身份证号" prop="idCard" align="center" />
      <el-table-column label="手机号" prop="phonenumber" align="center" />
      <el-table-column label="微信绑定手机号" prop="vxPhone" align="center" />
      <el-table-column
        label="详细地址"
        prop="address"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column label="注册时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最新登录时间" align="center" prop="loginDate">
        <template #default="scope">
          <span>{{ parseTime(scope.row.loginDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            text
            @click="handleDetail(scope.row)"
            v-hasPermi="['crowdInfo:crowd:detail']"
          >
            查询
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog
      title="群众信息"
      v-model="open"
      width="35vw"
      align-center
      append-to-body
    >
      <el-form ref="detailRef" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名：">{{ form.userName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型：">
              <el-tag type="success" v-if="form.residenceStatus == 0"
                >常住</el-tag
              >
              <el-tag type="warning" v-if="form.residenceStatus == 1"
                >暂住</el-tag
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号：">{{ form.idCard }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号：">{{ form.phonenumber }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信绑定手机号：">{{
              form.vxPhone
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址：">{{ form.address }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册时间：">{{
              form.createTime
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录记录：">{{ form.loginDate }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="close">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Crowd">
import { listCrowd, getCrowdById } from "@/api/crowdInfo/crowd";

const { proxy } = getCurrentInstance();

const tableList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const open = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    idCard: undefined,
    phonenumber: undefined,
    residenceStatus: "",
    userName: undefined,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询角色列表 */
function getList() {
  loading.value = true;
  listCrowd(queryParams.value).then((response) => {
    const { data } = response;
    tableList.value = data.records ?? [];
    total.value = data.total;
    loading.value = false;
  });
}

/** 查询按钮操作 */
function handleDetail(row) {
  getCrowdById({ id: row.crowdUserId }).then((response) => {
    form.value = response.data;
    open.value = true;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "crowd-user/exportCrowdExcel",
    {
      ...queryParams.value,
    },
    `群众基本信息表_${new Date().getTime()}.xlsx`
  );
}

/** 关闭按钮 */
function close() {
  open.value = false;
  form.value = {};
}

getList();
</script>
